/*
 * @Author: silence
 * @Date: 2022-01-14 16:16:22
 * @LastEditTime: 2022-05-03 08:43:47
 * @LastEditors: silence_szn 8475963+silence-szn@user.noreply.gitee.com
 * @Description: logger
 * @FilePath: /flutter_template_app/lib/utils/log/MyLog.dart
 */

import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';

class Mylog {
  static final Logger _myLogger = Logger(
    printer: PrefixPrinter(
        PrettyPrinter(
          methodCount: 0,
          errorMethodCount: 0,
          // printTime: true,
          colors: true,
          levelColors: {
            Level.trace: const AnsiColor.fg(9), //红色
            Level.debug: const AnsiColor.fg(249), //白色
            Level.info: const AnsiColor.fg(10), //绿色
            Level.warning: const AnsiColor.fg(11), //黄色
            Level.error: const AnsiColor.fg(9), //红色
          },
        ),
        trace: "跟踪",
        debug: "瞧瞧",
        info: "好消息",
        warning: "注意",
        error: "坏消息"),
  );

  static test() {
    for (int i = 0; i < 255; i++) {
      Logger tmplogger = Logger(
        printer: PrefixPrinter(
          PrettyPrinter(
            methodCount: 0,
            errorMethodCount: 0,
            printTime: true,
            colors: true,
            levelColors: {
              Level.info: AnsiColor.fg(i),
            },
          ),
        ),
      );
      tmplogger.i(i.toString());
    }
  }

  static void d(dynamic message) {
    if (!kReleaseMode) {
      _myLogger.d(message);
    }
  }

  static void i(dynamic message) {
    if (!kReleaseMode) {
      _myLogger.i(message);
    }
  }

  static void w(dynamic message) {
    if (!kReleaseMode) {
      _myLogger.w(message);
    }
  }

  static void e(dynamic message) {
    if (!kReleaseMode) {
      _myLogger.e(message);
    }
  }

  /**
   * @description: 打印请求的信息
   * @param url 接口地址，param 参数，result 结果
   * @return void
   */
  static void p(
      {String url = '',
      dynamic param,
      dynamic result,
      dynamic header,
      dynamic responseHeader,
      String? apiTime}) {
    Map<String, dynamic> map = {};

    if (url.isNotEmpty) {
      map['url'] = url;
    }

    if (!FFEmpty.stringIsEmpty(apiTime)) {
      map['apiTime'] = apiTime;
    }

    if (header != null) {
      map['header'] = header;
    }

    if (param != null) {
      map['param'] = param;
    }

    if (responseHeader != null) {
      map['responseHeader'] = responseHeader;
    }

    if (result != null) {
      map['result'] = result;
    }

    if (!kReleaseMode) {
      _myLogger.d(map);
    }
  }
}
